package io.mya.bian.controller;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.binance.api.client.BinanceApiClientFactory;
import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.domain.market.TickerPrice;

import io.mya.bian.entity.CurrencyEntity;
import io.mya.bian.service.BianService;
import io.mya.common.utils.R;

/**
 * 
 * @author renxin
 * 
 */
@RestController
@RequestMapping("/app/exchangeRate")
public class CurrencyController {
	@Autowired
	private BianService bianService;
	
	/**
	 * 获取所有币种
	 */
	@RequestMapping("/thenearfuture")
	public R thenearfuture() {
		String proxyHost = "127.0.0.1";
	    String proxyPort = "1080";
	    System.setProperty("http.proxyHost", proxyHost);
	    System.setProperty("http.proxyPort", proxyPort);
	    System.setProperty("https.proxyHost", proxyHost);
	    System.setProperty("https.proxyPort", proxyPort);
	    
		BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YXPeoSXzpkHpb5pUUoN81DPrqBr3jmSTv76DJTv53yKZI6agAkjHYHKlV9fitEe4", "ptPCSYuYbiCNX1FOJNVn0mujRQoKhBuVMvPQH6K7c6f61zZTQokpRatmZKbRpTf0");
		
		BinanceApiRestClient client = factory.newRestClient();
		 //这个接口是获取所有的最新价钱和代币名字
		 List<TickerPrice> allPrices = client.getAllPrices();
		 List<String> list = new ArrayList<>();
		 for (TickerPrice tickerPrice : allPrices) {
			list.add(tickerPrice.getSymbol());
		 }
		return R.ok().put("list", list);

	}
	/**
	 * 从数据库拿到数据
	 */
	@RequestMapping("/setting")
	public R findAll() {
		List<CurrencyEntity> list = bianService.queryList();
		return R.ok().put("list", list);
	}
	
	/**
	 * 操作币（增加或修改）
	 */
	@RequestMapping("/coinlis")
	public R coinlis(CurrencyEntity currencyEntity,String mid) {
		if(StringUtils.isEmpty(mid)){
			//增加
			bianService.insert(currencyEntity);
		}else{
			//修改
			Long id = Long.parseLong(mid);
			currencyEntity.setId(id);
			bianService.update(currencyEntity);
		}
		return R.ok();
	}
	
	
}
